﻿Imports utilitiesLibrary
Imports System.Data.SqlClient

Public Class flights
    Inherits System.Web.UI.Page

    Dim objDB As New DBConnect
    Dim count, I, flightNo As Integer
    Dim sql, Desc As String
    Dim ds, ds1 As DataSet
    Dim DisplayFlag_OneChecked As Boolean = False
    Dim DisplayFlag_GoodData As Boolean = True
    Dim dr As SqlDataReader


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")
        count = ViewState("count")

        If Not IsPostBack Then
            flightVisOff()
            sql = "Select * From Flights"
            ds = objDB.getDataSet(sql, count)
            ddFLights.DataSource = ds
            ddFLights.DataValueField = "FlightNumber"
            ddFLights.DataTextField = "FlightNumber"
            ddFLights.DataBind()

            'bind add flight boxes
            sql = "SELECT AirportCode FROM AirportCode"
            ds = objDB.getDataSet(sql, count)
            ddlDestCode.DataSource = ds
            ddlOrigin.DataSource = ds
            ddlDestCode.DataTextField = "AirportCode"
            ddlDestCode.DataValueField = "AirportCode"
            ddlOrigin.DataValueField = "AirportCode"
            ddlOrigin.DataTextField = "AirportCode"
            ddlDestCode.DataBind()
            ddlOrigin.DataBind()
            ViewState("count") = count
        End If
    End Sub

    Protected Sub btnAddFlight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddFlight.Click
        flightVisOn()
    End Sub

    Protected Sub btnRemFlight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRemFlight.Click
        Dim flightNo As Integer

        flightNo = CType(gvFlights.Rows(0).Cells(0).Text, Integer)
        sql = "DELETE FROM Flights WHERE FlightNumber=" & flightNo
        'MsgBox(sql)
        objDB.doUpdate(sql)
        Response.Redirect("flights.aspx")
    End Sub

    Protected Sub ddFLights_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddFLights.SelectedIndexChanged
        Try  'If the first item in DDList is selected it will result in a value of type Text
            lblFlightError.Visible = False
            gvFlights.Visible = True
            flightNo = ddFLights.SelectedItem.Value
            Desc = ddFLights.SelectedItem.Text
            ShowInfo(flightNo, Desc)
        Catch
            gvFlights.Visible = False
            lblFlightError.Visible = True
            lblFlightError.Text = "Do not select the first item in list"
        End Try
    End Sub

    Private Sub ShowInfo(ByVal flightNo As Integer, ByVal Desc As String)
        'lblValue.Text = "The Value of the Selected Item is " & ProdNo
        'lblText.Text = "The Text of the Selected Item is " & Desc
        sql = "Select * From Flights Where FlightNumber = '" & flightNo & "'"
        gvFlights.Columns(1).ItemStyle.Wrap = False
        gvFlights.DataSource = objDB.getDataSet(sql)
        gvFlights.DataBind()
    End Sub

    Protected Sub btnModFlight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnModFlight.Click
        Dim CheckFound As Boolean = False
        objDB = Session("db")
        Dim flightNo As Integer
        Dim TBox As TextBox
        'Dim Cbox As CheckBox
        Dim I As Integer
        Dim sql As String


        'check input gridview to see what flight to modify
        For I = 0 To (gvFlights.Rows.Count - 1)
            If gvFlights.Rows.Count <> 0 Then
                DisplayFlag_OneChecked = True
                TBox = CType(gvFlights.Rows(I).FindControl("txtDeparture"), TextBox)
                flightNo = CType(gvFlights.Rows(I).Cells(0).Text, String)

                If DisplayFlag_OneChecked Then
                    'modify flight stuff
                    sql = "UPDATE Flights SET DepartureTime='" & TBox.Text & "' WHERE FlightNumber=" & flightNo
                    'MsgBox(sql)
                    objDB.doUpdate(sql)
                    Response.Redirect("flights.aspx")
                Else
                    'make label display "Select Check box to modify flight"
                    lblFlightError.ForeColor = Drawing.Color.Red
                    lblFlightError.Text = "Please select a flight to modify or delete"
                    lblFlightError.Visible = True
                End If

            End If
        Next
    End Sub

    Private Sub flightVisOn()
        btnSubmitFlight.Visible = True
        btnCancelAddFlight.Visible = True
        lblAddDelFLight.Visible = True
        lblAddDest.Visible = True
        ddlDestCode.Visible = True
        lblAddOrigin.Visible = True
        ddlOrigin.Visible = True
        lblDepartTime.Visible = True
        txtDepartTime.Visible = True
        lblDistance.Visible = True
        txtDistance.Visible = True
        lblSeats.Visible = True
        txtSeats.Visible = True
    End Sub

    Private Sub flightVisOff()
        btnSubmitFlight.Visible = False
        btnCancelAddFlight.Visible = False
        lblAddDelFLight.Visible = False
        lblAddDest.Visible = False
        ddlDestCode.Visible = False
        lblAddOrigin.Visible = False
        ddlOrigin.Visible = False
        lblDepartTime.Visible = False
        txtDepartTime.Visible = False
        lblDistance.Visible = False
        txtDistance.Visible = False
        lblSeats.Visible = False
        txtSeats.Visible = False
    End Sub

    Protected Sub btnSubmitFlight_Click(sender As Object, e As EventArgs) Handles btnSubmitFlight.Click
        Dim orCode As String
        Dim destCode As String
        Dim departTime As String
        Dim dist As Integer
        Dim seats As Integer

        destCode = ddlDestCode.SelectedValue
        orCode = ddlOrigin.SelectedValue
        departTime = txtDepartTime.Text
        dist = txtDistance.Text
        seats = txtSeats.Text

        sql = "INSERT INTO Flights VALUES('" & orCode & "','" & destCode & "','" & departTime & "','" & dist & "','" & seats & "')"
        'MsgBox(sql)
        objDB.doUpdate(sql)
        sql = "Select @@Identity as Num"
        dr = objDB.getDataReader(sql)
        dr.Read()
        lblFlightError.Text = "New flight created with flight number:<b>" & dr("Num") & "</b>"
        lblFlightError.Visible = True
        dr.Close()
        Response.Redirect("flights.aspx")
        lblFlightError.Text = "New flight created with flight number:<b>" & dr("Num") & "</b>"
        lblFlightError.Visible = True

    End Sub

    Protected Sub btnCancelAddFlight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancelAddFlight.Click
        flightVisOff()
    End Sub
End Class